package cn.com.pajx.pajx_smart_platform.api;


import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import cn.com.pajx.pajx_smart_platform.mvp.model.IBaseModel;
import cn.com.pajx.pajx_smart_platform.tools.ToastUtil;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody;

/**
 * create by ruancw on 2023/04/03
 * description 数据请求处理
 */

public class HttpUtils {

    public static <T> Disposable getDisposable(Observable<HttpResponse<T>> observable, final String which, final IBaseModel.IOnRequestListener onRequestListener) {
        return observable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<HttpResponse<T>>() {
                    @Override
                    public void accept(HttpResponse<T> tHttpResponse) {
                        if (onRequestListener != null && tHttpResponse != null) {
                            //解决url路径中特殊字符转义问题
                            Gson gson = new GsonBuilder().disableHtmlEscaping().create();
                            String data = gson.toJson(tHttpResponse.getData());
                            String message = tHttpResponse.getMessage();
                            int code = tHttpResponse.getStatus();
                            if (code == 200 || code == 901 || code == 902) { // 901 登陆成功, 902 短信验证
                                onRequestListener.onRequestSuccess(data, message, code, which);
                            } else {
                                onRequestListener.onRequestError(message, code, which);
                            }
                        } else {
                            ToastUtil.toast("获取数据失败");
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) {
                        if (onRequestListener != null) {
                            ExceptionHandle.handleException(throwable);
                            throwable.printStackTrace();
                            onRequestListener.onRequestFail(throwable);
                        }
                    }
                });

    }

    /**
     * 下载文件
     *
     * @param observable        observable
     * @param which             tag
     * @param onRequestListener 网络请求监听接口
     */
    public static Disposable getFileDisposable(Observable<ResponseBody> observable, final String which, final IBaseModel.IOnRequestListener onRequestListener) {
        return observable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<ResponseBody>() {
                    @Override
                    public void accept(ResponseBody responseBody) {
                        if (onRequestListener != null && responseBody != null) {
                            onRequestListener.onDownloadSuccess(responseBody, which);
                        } else {
                            ToastUtil.toast("获取数据失败");
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) {
                        if (onRequestListener != null) {
                            ExceptionHandle.handleException(throwable);
                            throwable.printStackTrace();
                            onRequestListener.onRequestFail(throwable);
                        }
                    }
                });
    }
}
